class MVVM {
    constructor(options) {
        this.options = options
        let data = this.data = options.data

        Object.keys(data).forEach(key => {
            this.proxyData(key)
        })

        this.initComputed()
        observe(data, this)
        this.compile = new Compile(options.el, this)
    }

    watch(key, cb, options) {
        new Watcher(this, key, cb)
    }

    // 这个模式可以
    // 用 mvvm 代理了 mvvm.data 本身
    proxyData(key, setter, getter) {
        let that = this
        if (!setter) {
            Object.defineProperty(that, key, {
                configurable: false,
                enumerable: true,
                get: function proxyGetter() {
                    return that.data[key]
                },
                set: function proxySetter(newVal) {
                    that.data[key] = newVal
                }
            })
        }
    }

    initComputed() {
        let that = this
        let computed = this.options.computed
        if (typeof computed === 'object') {
            Object.keys(computed).forEach((key) => {
                Object.defineProperty(that, key, {
                    set: () => {
                        if (typeof computed[key] === 'function') {
                            return computed[key]
                        } else {
                            return computed[key].get
                        }
                    },
                    get: () => {
                    }
                })
            })
        }
    }
}
